package com.source.baseData.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineGrantAndRecovery.entity.HouseMachine;
import com.source.barcodeMachineGrantAndRecovery.service.HouseMachineService;
import com.source.barcodeMachineStatus.entity.BarcodeMachineStatus;
import com.source.barcodeMachineStatus.service.BarcodeMachineStatusService;
import com.source.baseData.entity.BarcodeMachine;
import com.source.baseData.service.BarcodeMachineService;
import com.source.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 脚标机的前端控制器
 * </p>
 *
 * @author 枫情
 * @since 2021-02-06
 */
@Api(tags = "脚标机基础数据管理 - 总表")
@RestController
@RequestMapping("/baseData/barcodeMachine")
@CrossOrigin
public class BarcodeMachineController {

    @Autowired
    private BarcodeMachineService barcodeMachineService;

    @Autowired
    private HouseMachineService houseMachineService;

    @Autowired
    private BarcodeMachineStatusService barcodeMachineStatusService;



    @ApiOperation(value = "添加一个脚标机的信息",notes = "未发放给市场")
    @PostMapping(value = "/save", produces = "application/json")
    public CommonResult<BarcodeMachine> save(@RequestBody @Validated BarcodeMachine barcodeMachine) {
        try {
            //默认未发放
            barcodeMachine.setDeliveryStatus("1");
            //保存
            barcodeMachineService.save(barcodeMachine);
            BarcodeMachineStatus barcodeMachineStatus = new BarcodeMachineStatus(null,
                    barcodeMachine.getBarcodeMachineNumber(),
                    null,
                    "0",
                    0,
                    Integer.MAX_VALUE,
//                    barcodeMachine.getUserId(),
                    //暂时用1代替
                    "1",
                    null,
                    null);
            barcodeMachineStatusService.save(barcodeMachineStatus);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<BarcodeMachine>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
        }
        return new CommonResult<BarcodeMachine>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachine);
    }

    @ApiOperation(value = "更新保存操作")
    @PutMapping("/updateById")
    public CommonResult<BarcodeMachine> updateById(@RequestBody @Validated BarcodeMachine barcodeMachine) {
        if (barcodeMachineService.updateById(barcodeMachine)) {
            return new CommonResult<BarcodeMachine>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachine);
        } else {
            return new CommonResult<BarcodeMachine>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
        }
    }

    @ApiOperation(value = "根据id进行删除记录")
    @DeleteMapping("/deleteById/{barcodeMachineId}")
    public CommonResult<Boolean> deleteById(@PathVariable("barcodeMachineId") String barcodeMachineId) {
        try {
            BarcodeMachine machine = barcodeMachineService.getById(barcodeMachineId);
            //ImageUtils.delete(machine.getImage());

            if (barcodeMachineService.removeById(barcodeMachineId)) {
                return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
            } else {
                return new CommonResult<Boolean>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(ResultEnum.PARAMS_ERROR.getCode(), ResultEnum.PARAMS_ERROR.getMsg(), false);
        }
    }


    @ApiOperation(value = "分页查询，current当前页数，size每一页的记录数")
    @CrossOrigin
    @GetMapping("/findAll/{current}/{size}")
    public CommonResult<Page> findAll(@PathVariable Integer current, @PathVariable Integer size) {
        List<BarcodeMachine> list = barcodeMachineService.list();
        Page page = MyPage.list2Page(current, size, list);
        return new CommonResult<Page>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "根据脚标机的id进行单个查询（这里不是用number查，是用于点击明细、编辑方面的回显查询）")
    @GetMapping("/getById/{id}")
    public CommonResult<BarcodeMachine> getOneVoById(@PathVariable("id") String id) {
        BarcodeMachine barcodeMachine = barcodeMachineService.getById(id);
        return new CommonResult<BarcodeMachine>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachine);
    }

    @GetMapping("/getPageByLike/{current}/{size}/{keyWord}")
    @ApiOperation("多字段模糊查询")
    public CommonResult<Page> getPageByLike(@PathVariable Integer current,
                                                            @PathVariable Integer size,
                                                            @PathVariable String keyWord) {

        if (null == keyWord) {
            return findAll(current, size);
        }
        List<BarcodeMachine> list = barcodeMachineService.getPageByLike(keyWord);
        Page page = MyPage.list2Page(current, size, list);
        return new CommonResult<Page>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), page);
    }

    @ApiOperation(value = "上传Excel，存储数据到数据库中")
    @PostMapping(value = "/uploadExcel", produces = "application/json;charset=utf-8")
    public CommonResult<Boolean> uploadExcel(@RequestPart(value = "excelFile", required = false) MultipartFile excelFile) throws IOException {
        if (null != excelFile && !excelFile.isEmpty()) {
            ExcelUtil.uploadExcel(excelFile, BarcodeMachine.class, barcodeMachineService);
            return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
        } else {
            return new CommonResult<Boolean>(ResultEnum.PARAMS_NULL.getCode(), ResultEnum.PARAMS_NULL.getMsg(), false);
        }
    }


//    @ApiOperation(value = "查询角标机信息,根据slaughterHouseId查询")
//    @GetMapping("/findBySlaughterHouseId/{SlaughterHouseId}")
//    public CommonResult<Page<BarcodeMachine>> findBySlaughterHouseId(@PathVariable("SlaughterHouseId") String SlaughterHouseId,
//                                                                     @RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageCurrent,
//                                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
//
//        List<BarcodeMachine> barcodeMachines = barcodeMachineService.findByHouseId(SlaughterHouseId);
//
//        Page<BarcodeMachine> page = MyPage.list2Page(pageCurrent, pageSize, barcodeMachines);
//        return new CommonResult<Page<BarcodeMachine>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), page);
//    }

//    @ApiOperation(value = "查询角标机信息,根据behavior查询")
//    @GetMapping("/findByBehavior")
//    public CommonResult<Page<BarcodeMachine>> findByBehavior(@RequestParam("behavior") String behavior,
//                                                             @RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageCurrent,
//                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
//        QueryWrapper<BarcodeMachine> wrapper = new QueryWrapper();
//        wrapper.eq("behavior", behavior);
//        List<BarcodeMachine> barcodeMachines = barcodeMachineService.list(wrapper);
//
//        Page<BarcodeMachine> page = MyPage.list2Page(pageCurrent, pageSize, barcodeMachines);
//        return new CommonResult<Page<BarcodeMachine>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), page);
//
//    }


//    @ApiOperation(value = "批量删除数据")
//    @DeleteMapping("/deleteBatch")
//    public CommonResult<Boolean> deleteBatch(@RequestParam("barcodeMachineIds") List<String> barcodeMachineIds) {
//        try {
//            List<BarcodeMachine> list = barcodeMachineService.listByIds(barcodeMachineIds);
//            for (BarcodeMachine barcodeMachine : list) {
//                ImageUtils.delete(barcodeMachine.getImage());
//            }
//            if (barcodeMachineService.removeByIds(barcodeMachineIds)) {
//                return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
//            } else {
//                return new CommonResult<Boolean>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), false);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new CommonResult<Boolean>(ResultEnum.PARAMS_ERROR.getCode(), ResultEnum.PARAMS_ERROR.getMsg(), false);
//        }
//    }

    /*@ApiOperation(value = "下载excel")
    @GetMapping(value = "/downloadExcel", produces = "application/vnd.ms-excel;charset=utf-8")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        ExcelUtil.downloadExcel(response, BarcodeMachine.class, barcodeMachineService);
    }*/


}

